package pers.wk.queues.topics;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import pers.wk.config.MQConfig;
import pers.wk.utils.MQConnectionUtils;

/**
 * 主题模式-发布订阅生产者
 */
public class TopicProducer {

    public static void main(String[] args) {

        //1.ConnectionFactory
        ConnectionFactory connectionFactory = MQConnectionUtils.getConnectionFactory("/");
        Connection connection = null;
        Channel channel = null;
        try {
            //2.创建连接
            connection = connectionFactory.newConnection();
            //3.通过连接获取通道Channel
            channel = connection.createChannel();
            //创建交换机
            channel.exchangeDeclare(MQConfig.ExchangeName.TOPIC_EXCHANGE, MQConfig.ExchangeType.topic);

            //String routingKey = "msg.info";
            String routingKey = "msg.info.sms";
            System.out.println("主题模式-生产者启动！");
            String msg = "[" + routingKey + "]RabbitMQ通配符模式的消息❤";
            System.out.println(msg);
            channel.basicPublish(MQConfig.ExchangeName.TOPIC_EXCHANGE, routingKey, null, msg.getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MQConnectionUtils.close(connection, channel);
        }
    }
}
